import data_course.YiShapeClient as yishape
import pandas as pd
import numpy as np

# 读取的行数量
num = 500
# 正类数据
f1 = r"E:\商业大数据\data\fakenews\True.csv"
df1 = pd.read_csv(f1)
s1 = df1.loc[:num, ['text']]
# 负类数据
f2 = r"E:\商业大数据\data\fakenews\Fake.csv"
df2 = pd.read_csv(f2)
s2 = df1.loc[:num, ['text']]
print(s1)
# 列表暂存向量特征
features = []
# 列表暂存标签
labels = []
# YiSahpe客户端，可以用于文本向量化
client = yishape.YiShapeClient()
# 遍历正负类中的文本数据、向量化，并加入特征和标签列表
for index, row in s1.iterrows():
    try:
        vec = client.text_embedding(row['text'])
        features.append(vec)
        labels.append("True")
        if index % 100 == 0:
            print("True:", index)
    except Exception as ex:
        print(ex)
for index, row in s2.iterrows():
    try:
        vec = client.text_embedding(row['text'])
        features.append(vec)
        labels.append("Fake")
        if index % 100 == 0:
            print("Fake:", index)
    except Exception as ex:
        print(ex)
# 基于向量列表创建DataFrame
df = pd.DataFrame(data=features)
# 为DataFrame创建表头（列名称）
cols = ['x' + str(i + 1) for i in np.arange(len(features[0]))]
df.columns = cols
# 添加标签列
df['label'] = labels
# 打乱行顺序
df = df.sample(frac=1.0).reset_index(drop=True)
df.to_csv(r"E:\商业大数据\data\text_vec.csv", index=False)
